<template>
    <div>
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item to="/home">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card shadow="always" :body-style="{ padding: '20px' }">
            <!-- 搜索框（表单） -->
            <el-row :gutter="10">
                <el-col :span="18">
                    <!-- inline表示都在一行 -->
                    <el-form  label-width="80px" :inline="true" size="small">
                        <el-form-item label="姓名">
                            <el-input v-model="params.trueName"></el-input>
                        </el-form-item>
                        <el-form-item label="电话">
                            <el-input v-model="params.phone"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" @click="searchBtn">查询</el-button>
                            <el-button type="primary" icon="el-icon-delete" @click="resetBtn">重置</el-button>
                            <el-button type="primary" icon="el-icon-plus" @click="addUser">添加</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>

            <!--  表格 -->
            <el-table :data="tableList" border stripe>
                <el-table-column type="index" label="序号" width="50px"></el-table-column>
                <el-table-column label="姓名" prop="trueName"></el-table-column>
                <el-table-column label="电话" prop="phone"></el-table-column>
                <el-table-column label="性别" prop="sex">
                    <template slot-scope="scope">
                        <el-tag type="danger" size="small" v-if="scope.row.sex == 0">女</el-tag>
                        <el-tag type="danger" size="small" v-if="scope.row.sex == 1">男</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="身份证号" prop="idCard"></el-table-column>
                <el-table-column label="用户名" prop="username"></el-table-column>
                <el-table-column label="身份" prop="isAdmin">
                    <template slot-scope="scope">
                        <el-tag type="danger" size="small" v-if="scope.row.isAdmin == 0">普通员工</el-tag>
                        <el-tag type="danger" size="small" v-if="scope.row.isAdmin == 1">超级管理员</el-tag>
                    </template>
                </el-table-column>

                <el-table-column label="在职状态" prop="status">
                    <template slot-scope="scope">
                        <!-- switch开关 -->
                        <!-- scope.row获取本行的数据 -->
                        <el-switch v-model="scope.row.status" :active-value="0" active-text="在职" :inactive-value="1"
                            inactive-text="离职" @change="changeUserStatus(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="是否可用" prop="enabled">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.enabled" :active-value="true" active-text="可用" :inactive-value="false"
                            inactive-text="禁用" @change="changeUserEnabled(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="300px" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" icon="el-icon-edit" size="small"
                            @click="editUser(scope.row)">编辑</el-button>
                        <el-button type="primary" icon="el-icon-delete" size="small"
                            @click="deleteUser(scope.row)">删除</el-button>
                        <el-button type="primary" icon="el-icon-edit" :disabled="scope.row.isAdmin == 1" size="small"
                            @click="assignRole(scope.row)">分配角色</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页 -->
            <el-pagination @size-change="sizeChange" @current-change="currentChange" :current-page.sync="params.currentPage"
                :page-sizes="[5, 10, 15, 20]" :page-size="params.pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="params.total" background>
                :pager-count="7">
            </el-pagination>


        </el-card>

        <!-- 添加或修改对话框 -->
        <el-dialog :title="editDialog.title" :visible.sync="editDialog.visible" :width="editDialog.width">

            <el-form :model="editModel" ref="editForm" :rules="rules" label-width="80px" :inline="true" size="small">
                <el-form-item label="姓名:" prop="trueName">
                    <el-input v-model="editModel.trueName"></el-input>
                </el-form-item>
                <el-form-item prop="sex" style="width: 280px" label="性别:">
                    <el-radio-group v-model="editModel.sex">
                        <el-radio :label="1">男</el-radio>
                        <el-radio :label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item prop="phone" label="电话:">
                    <el-input v-model="editModel.phone"></el-input>
                </el-form-item>
                <el-form-item prop="idCard" label="身份证:">
                    <el-input v-model="editModel.idCard"></el-input>
                </el-form-item>
                <el-form-item prop="username" label="用户名:">
                    <el-input v-model="editModel.username"></el-input>
                </el-form-item>
                <el-form-item prop="password" label="密码:" v-if="editModel.type == '0'">
                    <el-input type="password" v-model="editModel.password"></el-input>
                </el-form-item>
                <el-form-item prop="status" style="width: 280px" label="离职:">
                    <el-radio-group v-model="editModel.status">
                        <el-radio :label="0">在职</el-radio>
                        <el-radio :label="1">离职</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item prop="enabled" style="width: 280px" label="启用:">
                    <el-radio-group v-model="editModel.enabled">
                        <el-radio :label="true">是</el-radio>
                        <el-radio :label="false">否</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button @click="onClose">取 消</el-button>
                <el-button type="primary" @click="onConfirm">确 定</el-button>
            </div>

        </el-dialog>

        <!-- 分配角色对话框 -->
        <el-dialog :title="assignDialog.title" :visible.sync="assignDialog.visible" :width="assignDialog.width">

            <el-table :data="roleTableList" ref="roleTable" @selection-change="getAssignRoleIds" border stripe>
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column type="index" label="序号" width="50"></el-table-column>
                <el-table-column prop="roleName" label="角色名"></el-table-column>
                <el-table-column prop="remark" label="备注"></el-table-column>
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button @click="assignClose">取 消</el-button>
                <el-button type="primary" @click="assignConfirm">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
//导入sys-user中的方法
import { getUserListApi, addUserApi, editUserApi, deleteUserApi, checkRolesApi, saveRoleIdsByUserIdApi } from '@/assets/api/sys-user'

export default {
    name: 'WuyeSysUserList',

    data() {
        return {
            params: {
                trueName: '',
                phone: '',
                currentPage: 1,
                pageSize: 5,
                total: 0

            },
            //表格中的数据
            tableList: [],
            //对话框的数据
            //标题
            editDialog: {
                title: '',
                visible: false,
                width: '720px'
            },
            //表单数据绑定
            editModel: {
                userId: "",
                type: "", // 0:新增 1：编辑
                trueName: "",
                sex: "",
                phone: "",
                idCard: "",
                username: "",
                password: "",
                enabled: "",
                status: "",
            },
            rules: {
                trueName: [{
                    required: true,
                    trigger: "blur",
                    message: "请填写姓名",
                }],
                sex: [{
                    required: true,
                    trigger: "blur",
                    message: "请选择性别",
                }],
                phone: [{
                    required: true,
                    trigger: "blur",
                    message: "请填写电话",
                }],
                idCard: [{
                    required: true,
                    trigger: "blur",
                    message: "请填写身份证",
                }],
                username: [{
                    required: true,
                    trigger: "blur",
                    message: "请输入用户名",
                }],
                password: [{
                    required: true,
                    trigger: "blur",
                    message: "请输入密码",
                }],
                enabled: [{
                    required: true,
                    trigger: "blur",
                    message: "请选择是否禁用",
                }],
                status: [{
                    required: true,
                    trigger: "blur",
                    message: "请选择是否离职",
                }],
            },

            //分配角色对话框状态
            assignDialog: {
                title: '',
                visible: false,
                width: "800px"
            },
            //分配角色对话框数据
            roleTableList: [],
            //分配的用户id
            assignUserId: "",
            //分配的角色id
            assignRoleIds: []

        };
    },

    mounted() {
        this.getUserList();
    },


    methods: {
        //自定义的方法名
        async getUserList() {
            //let 声明用于声明可重新赋值的块级作用域局部变量
            //导入进来的方法
            //参数为params中的参数，分页搜索相关
            let res = await getUserListApi(this.params);
            if (res.code == 200) {
                this.tableList = res.data.records;
                this.params.total = res.data.total;
            }
        },
        searchBtn() {
            //重新渲染数据
            this.getUserList();
        },
        //重置方法
        resetBtn() {
            //清空搜索框的内容
            this.params.trueName = '';
            this.params.phone = '';
            //重新渲染数据
            this.getUserList();
        },
        //分页方法
        sizeChange(val) {
            this.params.pageSize = val;
            this.getUserList();
        },
        currentChange(val) {
            this.params.currentPage = val;
            this.getUserList();
        },
        //添加用户
        addUser() {
            //清空表单数据
            //参数一：哪个表单
            //参数二：哪些数据
            this.$resetForm("editForm", this.editModel)
            //设置编辑类型
            //type: "", // 0:新增 1：编辑
            this.editModel.type = "0";
            //设置标题
            this.editDialog.title = "新增员工";
            //显示对话框，默认false不显示
            this.editDialog.visible = true;
        },
        //编辑用户
        editUser(row) {
            //清空表单
            this.$resetForm("editForm", this.editModel);
            //设置对话框状态
            this.editModel.type = "1";
            //复制表格中的数据到对话框的表单中的数据
            this.$objCopy(row, this.editModel);
            // console.log(this.editModel)
            //设置标题
            this.editDialog.title = "编辑员工";
            this.editDialog.visible = true;
        },
        //关闭对话框的方法
        onClose() {
            this.editDialog.visible = false;
        },
        //确认对话框，保存用户数据
        onConfirm() {
            //校验表单中的数据
            this.$refs.editForm.validate(async (valid) => {
                if (valid) {
                    let res = null;
                    if (this.editModel.type == "0") {
                        //添加方法
                        res = await addUserApi(this.editModel)
                    } else {
                        //修改
                        res = await editUserApi(this.editModel);
                    }
                    if (res && res.code == 200) {
                        //渲染数据
                        this.getUserList();
                        //关闭对话框
                        this.editDialog.visible = false;
                    }
                }
            });
        },
        //删除用户
        async deleteUser(row) {
            // console.log(row.userId);
            let confirm = await this.$myconfirm("确认是否删除员工");
            if (confirm) {
                let res = await deleteUserApi(row.userId);
                if (res && res.code == 200) {
                    this.params.currentPage = 1;
                    this.getUserList();
                    this.$message.success(res.msg);
                }
            }
        },
        //修改用户在职离职状态
        async changeUserStatus(row) {
            // 为什么row
            let res = await editUserApi(row);
            if (res && res.code == 200) {
                this.getUserList();
                this.$message.success(res.msg);
            }
        },
        //修改用户的可用状态
        async changeUserEnabled(row) {
            let res = await editUserApi(row);
            if (res && res.code == 200) {
                this.getUserList();
                this.$message.success(res.msg);
            }
        },
        //用户分配角色
        async assignRole(row) {
            //展示对话框
            this.assignDialog.visible = true;
            //设置标题
            this.assignDialog.title = `为【${row.trueName}】分配角色`;
            let res = await checkRolesApi(row.userId);
            //设置角色列表数据
            if (res && res.code == 200) {
                this.roleTableList = res.data;
                this.$nextTick(() => {
                    for (let i = 0; i < this.roleTableList.length; i++) {
                        let role = this.roleTableList[i];
                        if (role.checked) {
                            this.$refs.roleTable.toggleRowSelection(this.roleTableList[i], true);

                        }
                    }
                })
            }
            this.assignUserId = row.userId;
        },
        getAssignRoleIds(value) {
            let roleIds = [];
            for (let i = 0; i < value.length; i++) {
                roleIds.push(value[i].roleId);
            }
            this.assignRoleIds = roleIds;
        },
        assignClose() {
            this.assignDialog.visible = false;
        },
        async assignConfirm() {
            let param = {
                userId: this.assignUserId,
                roleIds: this.assignRoleIds
            }
            let res = await saveRoleIdsByUserIdApi(param);
            this.assignDialog.visible = false;
        }
    },
};
</script>

<style lang="scss" scoped></style>